package dao;

import entity.DiaryInfo;
import util.DBUtill;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DiaryDao {
     //保存日记
    public void save(DiaryInfo di){
        String sql="insert into diaryinfo(userId,diaryDay,weather,mood,contents,reportTime) values(?,?,?,?,?,?)";
        DBUtill.update(sql,di.toArray());
    }
    //修改日记
    public void update(DiaryInfo di){
        String sql="update diaryinfo set weather=?,mood=?,contents=?,reportTime=? where id=?";
        DBUtill.update(sql,di.getWeather(),di.getMood(),di.getContents(),di.getReportTime(),di.getId());
    }
    //删除记录
    public void delete(DiaryInfo di){
        String sql="delete  from diaryinfo where id=?";
        DBUtill.update(sql,di.toArray());
    }
    //通过ID查找记录
    public DiaryInfo findById(Integer id){
        String sql="select * from diaryinfo where id=?";
        ResultSet rs=DBUtill.find(sql,id);
        try {
            rs.next();
            DiaryInfo di = getDiaryInfo(rs);
            return di;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //查找所有记录
    public List<DiaryInfo> findAll(){
        List<DiaryInfo> list=new ArrayList<>();
        try {
            String sql="select * from diaryinfo";
            ResultSet rs=DBUtill.find(sql);
            while (rs.next()){
               // System.out.println("ceshi");
                DiaryInfo di = getDiaryInfo(rs);
                list.add(di);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    private DiaryInfo getDiaryInfo(ResultSet rs) throws SQLException {
        DiaryInfo di=new DiaryInfo();
        di.setId(rs.getInt("id"));
        di.setUserId(rs.getInt("userId"));
        di.setDiaryDay(rs.getDate("diaryDay"));
        di.setMood(rs.getString("mood"));
        di.setContents(rs.getString("contents"));
        di.setWeather(rs.getString("weather"));
        di.setReportTime(rs.getTime("reportTime"));
        return di;
    }
  public static void main(String[] agrs){
      DiaryDao dd = new DiaryDao();
      DiaryInfo di=dd.findById(17);
      dd.delete(di);
//////          DiaryDao dd= new DiaryDao();
////////         DiaryInfo di=dd.findById(1);
////////        di.setContents("我想说点什么？");
////////        //dd.update(di);//修改数据
////////        System.out.println(di);
//////        List<DiaryInfo> lists=dd.findAll();
//////        for (DiaryInfo d:lists){
//////            System.out.println(d);
//////        }
//        //添加数据
//        DiaryInfo di=new DiaryInfo();
//        di.setUserId(8);
//        di.setDiaryDay(new Date());
//        di.setWeather("晴");
//        di.setMood("差");
//        di.setContents("时间就是不能能不能报销直接和包装科技直接回复就不女嘉宾和张柏芝*******************************************************************************************************8ufhdjfdjughivbfjiahu");
//        di.setReportTime(new Date());
//        DiaryDao dd=new DiaryDao();
//        dd.save(di);
    }
}
